###############################################################
# Values, Taboos, and Votes: 
# How Basic Human Values Affect Populist Electoral Support
#
# Ugur Ozdemir, Marc S. Jacob
# 2021
# SUPPLEMENTAL MATERIAL I
###############################################################

# Packages and data -------------------------------------------------------

# install.packages(c("gridExtra","car","tidyverse","data.table","foreign","lavaan","memisc","stargazer"),dependencies = TRUE)

pckgs <-
  c(
    "gridExtra",
    "car",
    "tidyverse",
    "data.table",
    "foreign",
    "lavaan",
    "memisc",
    "stargazer"
  )
lapply(pckgs, require, character.only = TRUE)

# loading data

gr17 <- read.spss("ZA6804_v6-0-0.sav",to.data.frame = TRUE,use.value.labels = FALSE) # loading data
gr17 <- filter(gr17, w1b == "1") # select observations from wave 1

sem.data <-
  dplyr::select(
    gr17,
    kp1_170,
    kp1_2100,
    kp1_2090a,
    kp1_190ba,
    kp1_1090,
    kp1_1130,
    kp1_1210,
    kp1_1250,
    kp1_1290,
    kp1_1411,
    kp1_2591,
    kp1_3910,
    kp1_2320,
    kpx_2290,
    kpx_2280,
    kp1_1500,
    contains("3320"),
    kp1_3680,
    kp1_2580,
    kp9_200ba,
    ostwest
  ) # select variables

#### VARIABLE PREPARATION ####

# rename variables #

oldnames <-
  c(
    "kp1_1090",
    "kp1_3910",
    "kp1_2320",
    "kp1_2591",
    "kpx_2280",
    "kp1_2580",
    "kp1_1411",
    "kp1_1130",
    "kp1_1290",
    "kp1_1250",
    "kp1_1210",
    "kp1_1500"
  ) # rename variables
newnames <-
  c(
    "SocEcon1",
    "Relig",
    "Educ",
    "Income",
    "Gender",
    "Class",
    "SecPriv1",
    "Immigrat1",
    "Climate1",
    "EuInteg1",
    "MigInteg1",
    "LeftRight"
  )
setnames(sem.data, oldnames, newnames)

# data manipulation #

sem.data$Age <- 2016 - sem.data$kpx_2290 # compute age variable

sem.data$Relig <-
  recode(sem.data$Relig, "1=5;2=4;3=3;4=2;5=1") #recoding religion (5 (new) = very religious)
sem.data$Gender <-
  recode(sem.data$Gender, "2=1;1=0") #recoding gender (1 (new) = female)

# set party preference #

sem.data$AfD <- ordered(ifelse(sem.data$kp1_190ba == 322, 1, 0))
sem.data$Left <- ordered(ifelse(sem.data$kp1_190ba == 7, 1, 0))
sem.data$SPD <- ordered(ifelse(sem.data$kp1_190ba == 4, 1, 0))
sem.data$Greens <- ordered(ifelse(sem.data$kp1_190ba == 6, 1, 0))
sem.data$CDU <- ordered(ifelse(sem.data$kp1_190ba == 1, 1, 0))
sem.data$FDP <- ordered(ifelse(sem.data$kp1_190ba == 5, 1, 0))

sem.data$populist <- memisc::recode(sem.data$kp1_190ba,
                                    1 <- c(322, 7),
                                    NA <- NA,
                                    otherwise = 0)

# set issue opinions ordered (for ordinal regression of values on issue opinions) #

sem.data$Immigrat1 <- ordered(sem.data$Immigrat1)
sem.data$SecPriv1 <- ordered(sem.data$SecPriv1)
sem.data$EuInteg1 <- ordered(sem.data$EuInteg1)
sem.data$SocEcon1 <- ordered(sem.data$SocEcon1)
sem.data$MigInteg1 <- ordered(sem.data$MigInteg1)
sem.data$Climate1 <- ordered(sem.data$Climate1)
sem.data$LeftRight <- as.numeric(sem.data$LeftRight)

# split sample East/West

west <- filter(sem.data, ostwest == "1") # west
east <- filter(sem.data, ostwest == "0") # east

####DESCRIPIVE STATISTICS####

sem.data %>% dplyr::select(
  AfD,
  Left,
  SPD,
  Greens,
  CDU,
  FDP,
  kp1_3320e,
  kp1_3320m,
  kp1_3320c,
  kp1_3320h,
  kp1_3320u,
  kp1_3320n,
  kp1_3320q,
  Immigrat1,
  SecPriv1,
  EuInteg1,
  SocEcon1,
  MigInteg1,
  Climate1,
  Age,
  Income,
  Gender,
  Educ,
  Relig,
  ostwest,
  LeftRight
) %>% mutate_all(as.numeric) -> descr

stargazer(
  descr,
  type = "latex",
  covariate.labels = c(
    "AfD vote",
    "The Left vote",
    "SPD vote",
    "Greens vote",
    "CDU vote",
    "FDP vote",
    "Security 1",
    "Security 2",
    "Universalism 1",
    "Universalism 2",
    "Universalism 3",
    "Conformity 1",
    "Conformity 2",
    "Immigration",
    "Privacy",
    "European integration",
    "Socio-economy",
    "Integration",
    "Climate",
    "Age",
    "Income",
    "Gender",
    "Educational level",
    "Religiosity",
    "East/west-born",
    "Left-right"
  ),
  digits = 2
)


####EAST/WEST SEPARATE####

####WEST####

###CDU###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
CDU ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1
+ b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec
+ Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'
fitCDUwest <- sem(model, data = west)


###SPD###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
SPD ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitSPDwest <- sem(model, data = west)


###AfD###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
AfD ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitAfDwest <- sem(model, data = west)


###Greens###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
Greens ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income+ Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income+ Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income+ Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income+ Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income+ Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income+ Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitGreenswest <- sem(model, data = west)

###Left###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
Left ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income+ Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income+ Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income+ Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income+ Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income+ Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income+ Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitLeftwest <- sem(model, data = west)

###FDP###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
FDP ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitFDPwest <- sem(model, data = west)

### SUMMARY SEM MODELS ###

fitMeasures(fitAfDwest, c("chisq", "df", "cfi", "tli", "rmsea")) #AfD
summary(fitAfDwest, standardized = TRUE, fit.measures = TRUE)

fitMeasures(fitLeftwest, c("chisq", "df", "cfi", "tli", "rmsea")) #Left
summary(fitLeftwest,
        standardized = TRUE,
        fit.measures = TRUE)

fitMeasures(fitCDUwest, c("chisq", "df", "cfi", "tli", "rmsea")) #CDU
summary(fitCDUwest, standardized = TRUE, fit.measures = TRUE)

fitMeasures(fitSPDwest, c("chisq", "df", "cfi", "tli", "rmsea")) #SPD
summary(fitSPDwest, standardized = TRUE, fit.measures = TRUE)

fitMeasures(fitGreenswest, c("chisq", "df", "cfi", "tli", "rmsea")) #Greens
summary(fitGreenswest,
        standardized = TRUE,
        fit.measures = TRUE)

fitMeasures(fitFDPwest, c("chisq", "df", "cfi", "tli", "rmsea")) #FDP
summary(fitFDPwest, standardized = TRUE, fit.measures = TRUE)

####EAST####

###CDU###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
CDU ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1
+ b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec
+ Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'
fitCDUeast <- sem(model, data = east)


###SPD###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
SPD ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitSPDeast <- sem(model, data = east)


###AfD###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
AfD ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitAfDeast <- sem(model, data = east)


###Greens###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
Greens ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income+ Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income+ Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income+ Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income+ Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income+ Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income+ Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitGreenseast <- sem(model, data = east)

###Left###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
Left ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income+ Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income+ Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income+ Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income+ Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income+ Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income+ Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitLefteast <- sem(model, data = east)

###FDP###

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
FDP ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitFDPeast <- sem(model, data = east)

### SUMMARY SEM MODELS ###

fitMeasures(fitAfDeast, c("chisq", "df", "cfi", "tli", "rmsea")) #AfD
summary(fitAfDeast, standardized = TRUE, fit.measures = TRUE, std.nox=TRUE)

fitMeasures(fitLefteast, c("chisq", "df", "cfi", "tli", "rmsea")) #Left
summary(fitLefteast,
        standardized = TRUE,
        fit.measures = TRUE)

fitMeasures(fitCDUeast, c("chisq", "df", "cfi", "tli", "rmsea")) #CDU
summary(fitCDUeast, standardized = TRUE, fit.measures = TRUE, std.nox=TRUE)

fitMeasures(fitSPDeast, c("chisq", "df", "cfi", "tli", "rmsea")) #SPD
summary(fitSPDeast, standardized = TRUE, fit.measures = TRUE, std.nox=TRUE)

fitMeasures(fitGreenseast, c("chisq", "df", "cfi", "tli", "rmsea")) #Greens
summary(fitGreenseast,
        standardized = TRUE,
        fit.measures = TRUE)

fitMeasures(fitFDPeast, c("chisq", "df", "cfi", "tli", "rmsea")) #FDP
summary(fitFDPeast, standardized = TRUE, fit.measures = TRUE, std.nox=TRUE)


####SEM MODEL POPULIST (AFD + THE LEFT)####

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
populist ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1
+ b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec
+ Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'
fitpopulist <- sem(model, data = sem.data)

fitMeasures(fitpopulist, c("chisq", "df", "cfi", "tli", "rmsea"))
summary(fitpopulist,
        standardized = TRUE,
        fit.measures = TRUE, 
        std.nox=TRUE)